package com.mySampleApplication.client.codeRule;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mySampleApplication.client.components.SDialog;
import com.mySampleApplication.client.dmp.DmpDto;
import com.mySampleApplication.client.dmp.DmpService;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Dialog;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.TreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeRuleFilterForm {
        public static DynamicForm getFilterForm(DataSource dataSource, ListGrid listGrid){
            final DynamicForm filterForm = new DynamicForm();
            filterForm.setWidth(600);
            filterForm.setNumCols(4);
            filterForm.setDataSource(dataSource);
            TextItem modelItemId= new TextItem( "数据模型Id");
            modelItemId.setVisible(false);
            TextItem modelItem= new TextItem( "数据模型");
            ListGridRecord record=new ListGridRecord();
            modelItem.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
                @Override
                public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent clickEvent) {
                    DmpService.App.getInstance().getDataElementCategoryList(new AsyncCallback<DmpDto>() {
                        public void onSuccess(DmpDto dmpDto) {
                            List<Map<String, String>> keyValueMap = dmpDto.getKeyValueListMap();
                            TreeNode[] employeeData = new TreeNode[1];
                            int i=0;
                            for (Map<String, String> data : keyValueMap) {
                                employeeData[i]= new CodeRule.EmployeeTreeNode(data.get("id"),data.get("parent_id"),data.get("name"));
                                i++;
                            }
//                            modelItem.setValue(record.getAttribute("sjdxid"));
//                            record.setAttribute("sjdxid","1");
                            Window dialog =dataObjectwindow.getShowWindow(1200,550,"(1/5)选择数据对象",record,listGrid,employeeData,modelItem, modelItemId);
                            dialog.show();
                        }
                        public void onFailure(Throwable caught) {
                            SC.warn("获取数据失败！");
                        }
                    });
                }
            });


            TextItem nameItem = new TextItem("规则名称");
//            DmpService.App.getInstance().selectObjectList(new AsyncCallback<DmpDto>() {
//                public void onSuccess(DmpDto result) {
//                    Map<String, String> keyValueMap = result.getKeyValueMap();
//                    // 设置静态数据
//                    modelItem.setValueMap(keyValueMap);
//                }
//                public void onFailure(Throwable caught) {
//                    SC.warn("获取数据失败！");
//                }
//            });
            filterForm.setFields(modelItem, nameItem,modelItemId);
            return filterForm;
        }
        public static HLayout getSerchButton(ListGrid listGrid, DynamicForm filterForm){
            HLayout hLayout = new HLayout(10);
            hLayout.setHeight(22);
            VLayout layout = new VLayout(); // 或者使用 HLayout
            Button filterButton = new Button("Filter");
            filterButton.setTitle("搜索");
//            filterButton.setWidth(60);
            filterButton.setShowRollOver(true);
            filterButton.setShowDown(true);
//            filterButton.setIcon("[SKIN]/actions/view_Disabled.png"); // 设置图标路径
            filterButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    Map<String, String> mapPage = new HashMap<>();
                    mapPage.put("pageSize", "1");
                    mapPage.put("pageNumber", "10");
                    mapPage.put("db_model_id", filterForm.getValueAsString("数据模型Id"));
                    mapPage.put("mc", filterForm.getValueAsString("规则名称"));

                    Map<String, String> parameter = new HashMap<>();
                    if( filterForm.getValueAsString("数据模型Id")!=null){
                        parameter.put("db_model_id", filterForm.getValueAsString("数据模型Id"));
                    }
                    if(filterForm.getValueAsString("规则名称")!=null){
                        parameter.put("mc", filterForm.getValueAsString("规则名称"));
                    }
                    List<ListGridRecord> recordList = new ArrayList<>();
                    CodeRuleService.App.getInstance().getSelect(parameter, mapPage, new AsyncCallback<CodeRuleDto>() {
                        public void onSuccess(CodeRuleDto result) {
                            List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                            for (Map<String, String> data : keyValueMap) {
                                ListGridRecord record = new ListGridRecord();
                                data.forEach((k, v) -> {
                                    record.setAttribute(k, v);
                                });
                                // 设置其他需要的属性
                                recordList.add(record);
                            }
                            listGrid.setData(recordList.toArray(new ListGridRecord[0]));
                        }

                        public void onFailure(Throwable caught) {
                            SC.warn("获取数据失败！");
                        }
                    });
//                    listGrid.fetchData(filterForm.getValuesAsCriteria());
                }
            });
            filterButton.setAutoFit(true);

            IButton clearButton = new IButton("Clear");
            clearButton.setTitle("重置");
//            clearButton.setIcon("[SKIN]/actions/refresh.png"); // 设置图标路径
            clearButton.setAutoFit(true);
            clearButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    listGrid.fetchData();
                    filterForm.clearValues();
                }
            });

            // 创建按钮和事件处理
            Button addButton = new Button("Add");
            addButton.setTitle("新增");
            addButton.setWidth(40);
//            addButton.setIcon("[SKIN]/actions/add_Disabled.png"); // 设置图标路径
            addButton.setShowRollOver(true);
            addButton.setShowDown(true);
            addButton.addClickHandler(event -> CodeRule.showAddDialog(listGrid));
            Button ckscButton = new Button("编码规范");
            ckscButton.setTitle("编码规范");
            ckscButton.setWidth(100);
//            ckscButton.setIcon("[SKIN]/actions/view_rtl.png"); // 设置图标路径
            ckscButton.setShowRollOver(true);
            ckscButton.setShowDown(true);
            ckscButton.addClickHandler(event -> CkscWindow.getShowWindow(1000,350,"编码规范", null,listGrid).show());
            layout.setDefaultLayoutAlign(Alignment.RIGHT);
            hLayout.addMember(filterButton);
            hLayout.addMember(clearButton);
            hLayout.addMember(addButton);

            hLayout.addMember(layout);
            return hLayout;
        }
    private static void showqDialog(ListGrid listGrid) {
        Dialog dialog = SDialog.createDialog(1400,650,"表单组件", null,listGrid);
        dialog.show();
    }
    }
